package org.walklink.core.event;

import com.google.common.collect.Maps;
import org.walklink.common.common.AbstractLazyInitializer;
import org.walklink.common.enums.EventTypeEnum;
import java.util.Map;

/**
 * 处理处理策略类
 *
 * @author lidishan
 */
public abstract class EventHandlerStrategy {

    /** 初始化 **/
    private static final AbstractLazyInitializer<Map<EventTypeEnum, IEventHandler>> MAP =
            new AbstractLazyInitializer<Map<EventTypeEnum, IEventHandler>>() {
                @Override
                protected Map<EventTypeEnum, IEventHandler> initialize() {
                    Map<EventTypeEnum, IEventHandler> result = Maps.newHashMap();
                    // 手动注册实现类，感觉有点蠢这样
                    result.put(EventTypeEnum.HEARTBEAT, new HeartBeatEventHandler());
                    result.put(EventTypeEnum.TOPIC_QUERY, new TopicQueryEventHandler());
                    result.put(EventTypeEnum.JVM_SYN, new JvmEventHandler());

                    return result;
                }
            };

    /**
     * 根据枚举值获取策略
     * @param typeEnum 枚举值
     * @return handler
     */
    public static IEventHandler select(EventTypeEnum typeEnum) {
        return MAP.get().getOrDefault(typeEnum, new EmptyEventHandler());
    }


}
